﻿
Partial Class WebForm_SelfManageWorkHeader_SelfAddWorkHeader
    Inherits System.Web.UI.UserControl
    Public rboOther As String = "9" '髒亂地區類別(其他)
    Public gvDirtyItemID As String = "7" '髒亂種類/程度(其他)

    Protected Sub Page_Init(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Init
        Me.fv.InsertItemTemplate = Me.fv.ItemTemplate
        Me.fv.EditItemTemplate = Me.fv.ItemTemplate
    End Sub

    Protected Sub Page_Load(ByVal sender As Object, ByVal e As System.EventArgs) Handles Me.Load

    End Sub

    Protected Sub rdoLocationType_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Dim rdo As RadioButtonList = sender
        TryCast(Me.fv.FindControl("mvReportDirtyWilling"), MultiView).ActiveViewIndex = rdo.SelectedIndex
    End Sub

    Protected Sub ddlTownID_SelectedIndexChanged(ByVal sender As Object, ByVal e As System.EventArgs)
        Me.BindVillageID()
    End Sub

    Protected Sub fv_DataBound(ByVal sender As Object, ByVal e As System.EventArgs) Handles fv.DataBound
        Me.BindTownID()
        Me.BindDirtyItemForInsert()
        TryCast(Me.fv.FindControl("SelfRegionType1"), WebForm_SelfManageWorkHeader_SelfRegionType).BindgvRegionType()

        If Me.fv.CurrentMode <> FormViewMode.Insert Then
            If Me.fv.DataItem IsNot Nothing Then
                '清查地點
                Dim ddlItem As DropDownList
                ddlItem = TryCast(Me.fv.FindControl("ddlTownID"), DropDownList)
                ddlItem.SelectedIndex = ddlItem.Items.IndexOf(ddlItem.Items.FindByValue(DataBinder.Eval(Me.fv.DataItem, "RegionID").ToString()))
                BindVillageID()

                ddlItem = TryCast(Me.fv.FindControl("ddlVillageID"), DropDownList)
                ddlItem.SelectedIndex = ddlItem.Items.IndexOf(ddlItem.Items.FindByValue(DataBinder.Eval(Me.fv.DataItem, "VillageID").ToString()))

                '髒亂地區類別
                'TryCast(Me.fv.FindControl("SelfRegionType1"), WebForm_SelfManageWorkHeader_SelfRegionType).BindRegionItem(DataBinder.Eval(Me.fv.DataItem, "RegionType").ToString(), DataBinder.Eval(Me.fv.DataItem, "Location").ToString())
                TryCast(Me.fv.FindControl("SelfRegionType1"), WebForm_SelfManageWorkHeader_SelfRegionType).BindRegionItem(DataBinder.Eval(Me.fv.DataItem, "RegionType").ToString())

                'robItem = robItem.Items.IndexOf(robItem.Items.FindByText(DataBinder.Eval(Me.fv.DataItem, "RegionType").ToString()))
                ''其他
                'If robItem.SelectedIndex = -1 Then
                '    robItem.SelectedIndex = robItem.Items.IndexOf(robItem.Items.FindByValue(Me.rboOther))
                '    TryCast(Me.fv.FindControl("txtOtherRegionType"), TextBox).Text = DataBinder.Eval(Me.fv.DataItem, "RegionType").ToString()
                'End If
            End If

            If Me.fv.CurrentMode = FormViewMode.ReadOnly Then
                TryCast(Me.fv.FindControl("ddlTownID"), DropDownList).Enabled = False
                TryCast(Me.fv.FindControl("ddlVillageID"), DropDownList).Enabled = False
                TryCast(Me.fv.FindControl("txtLocation"), TextBox).Enabled = False
                TryCast(Me.fv.FindControl("rboRegionType"), WebForm_SelfManageWorkHeader_SelfRegionType).Enable(False)
                TryCast(Me.fv.FindControl("txtOtherRegionType"), TextBox).Enabled = False
                TryCast(Me.fv.FindControl("txtReportDate"), TextBox).Enabled = False
                TryCast(Me.fv.FindControl("btnSave"), Button).Visible = False
            End If

        End If
    End Sub

    Protected Sub gv_RowDataBound(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.GridViewRowEventArgs)

        If e.Row.RowType = DataControlRowType.DataRow Then

            Dim item As New ListItem
            item.Text = TryCast(e.Row.FindControl("lblSubText"), Label).Text
            item.Value = TryCast(e.Row.FindControl("lblSubValue"), Label).Text
            TryCast(e.Row.FindControl("cbList"), CheckBoxList).Items.Add(item)

            Me.BindDirtyLevel(TryCast(e.Row.FindControl("ddlDirtyLevel"), DropDownList))

            If Me.fv.CurrentMode = FormViewMode.ReadOnly Then
                TryCast(e.Row.FindControl("ddlDirtyLevel"), DropDownList).Enabled = False
            End If
        End If
    End Sub

    Protected Sub fv_ItemCommand(ByVal sender As Object, ByVal e As System.Web.UI.WebControls.FormViewCommandEventArgs) Handles fv.ItemCommand

        'Dim RegionOther As String = TryCast(TryCast(TryCast(Me.fv.FindControl("SelfRegionType1"), WebForm_SelfManageWorkHeader_SelfRegionType).FindControl("gvRegionType"), GridView).Rows(4).FindControl("txtOther"), TextBox).Text.Trim()

        If e.CommandName = "btnSave" Then
            '髒亂地區類別,[其他]而未填入值 
            'If TryCast(Me.fv.FindControl("SelfRegionType1"), WebForm_SelfManageWorkHeader_SelfRegionType).RegionID = Me.rboOther And RegionOther = "" Then
            '    MyObj.ShowMessage(Me.GetLocalResourceObject("MustOtherRegionType"))
            'Else

            '髒亂種類/程度選擇,沒有做任何選擇
            Me.dirtyItemField = SetDirtyItem()

            If Not HaveItem Then
                MyObj.ShowMessage(Me.GetLocalResourceObject("MustDirtyItemName"))
                Return
            End If
            If Not Me.OtherDirtyItemName() Then '髒亂種類/程度選擇,[其他]而未填入值
                MyObj.ShowMessage(Me.GetLocalResourceObject("MustOtherDirtyItemName"))
            Else
                RaiseEvent Saving(Me, New EventArgs)
            End If
            'End If
        End If
    End Sub

    Protected Sub gvDirtyItem_DataBound(ByVal sender As Object, ByVal e As System.EventArgs)
        '將項目名稱相同者做 RowSpan
        Dim i As Integer = 1

        Dim GridView_Master As GridView = TryCast(Me.fv.FindControl("gvDirtyItem"), GridView)


        For Each wkItem As GridViewRow In GridView_Master.Rows
            If wkItem.RowIndex <> 0 Then
                Dim firstSubRow As Integer = wkItem.RowIndex - i
                Dim s1 As String = DirectCast(wkItem.FindControl("lblDirtyItemName"), Label).Text.Trim()
                Dim s2 As String = DirectCast(GridView_Master.Rows(firstSubRow).FindControl("lblDirtyItemName"), Label).Text.Trim()

                If s1 = s2 Then
                    GridView_Master.Rows(firstSubRow).Cells(0).RowSpan += 1
                    wkItem.Cells(0).Visible = False
                    i = i + 1
                Else
                    GridView_Master.Rows((wkItem.RowIndex)).Cells(0).RowSpan += 1

                    i = 1

                End If
            Else

                wkItem.Cells(0).RowSpan = 1
            End If
        Next


    End Sub

    Public Sub SetData(ByVal CaseNo As String)
        Me.InitData()

        Dim dat As New clsData
        Dim sql As New MyStringBuilder
        Dim dt As Data.DataTable

        sql.Length = 0
        sql.AppendFormat("Select Isnull(ReportDate,GetDate()) as ReportDate,LocationB,LocationC,ReportDescription,ItemName,Count ")
        sql.AppendFormat(",Replace(Replace(Isnull(Address,''),LocationB,''),LocationC,'') as NewAddress from EcolifeDataView ")
        sql.AppendFormat(" where CaseNo='{0}' ", CaseNo)
        dt = dat.GetData(sql)

        For i As Integer = 0 To dt.Rows.Count - 1
            If i = 0 Then
                TryCast(Me.fv.FindControl("txtLocation"), TextBox).Text = dt.Rows(i)("NewAddress").ToString()
                TryCast(Me.fv.FindControl("txtReportDate"), WebForm_CommonControl_CalendarTextBox).Value = Convert.ToDateTime(dt.Rows(i)("ReportDate")).Date
                Me.MyMatchItem(TryCast(Me.fv.FindControl("ddlTownID"), DropDownList), dt.Rows(i)("LocationB").ToString())
                Me.BindVillageID()
                Me.MyMatchItem(TryCast(Me.fv.FindControl("ddlVillageID"), DropDownList), dt.Rows(i)("LocationC").ToString())
                TryCast(Me.fv.FindControl("txtDescription"), TextBox).Text = dt.Rows(i)("ReportDescription").ToString()
            End If

            Dim gvDirtyItem As GridView = Me.fv.FindControl("gvDirtyItem")
            For j As Integer = 0 To gvDirtyItem.Rows.Count - 1
                If TryCast(gvDirtyItem.Rows(j).FindControl("lblSubText"), Label).Text.IndexOf(dt.Rows(i)("ItemName").ToString()) <> -1 Then
                    TryCast(gvDirtyItem.Rows(j).FindControl("txtNum"), TextBox).Text = dt.Rows(i)("Count").ToString()
                    Exit For
                End If
            Next
        Next
    End Sub

    Public Sub MyMatchItem(ByVal Item As DropDownList, ByVal Text As String)
        Item.SelectedIndex = Item.Items.IndexOf(Item.Items.FindByText(Text.Trim()))
    End Sub
End Class

Partial Class WebForm_SelfManageWorkHeader_SelfAddWorkHeader

#Region "初始化資料"
    '新增
    Public Sub InitData()
        Me.DataAccessMode = My.DataAccessMode.Insert
        'Me.fv.ChangeMode(FormViewMode.ReadOnly)
        Me.fv.ChangeMode(FormViewMode.Insert)
        Me.fv.DataBind()
    End Sub

    '編輯
    Public Sub InitData(ByVal FindDirtyHeaderID As String, ByVal Status As String)

        Dim sql As New MyStringBuilder
        Dim dat As New clsData

        sql.AppendFormat("select a.*,b.RegionID, wh.Active from FindDirtyHeader a")
        sql.AppendFormat(" join Region b on Left(a.VillageID,3)=b.RegionID ")
        sql.AppendFormat(" and b.RegionType='T' ")
        sql.AppendFormat("left join WorkHeader wh on a.WorkHeaderID = wh.WorkHeaderID ")
        sql.AppendFormat(" where a.FindDirtyHeaderID='{0}' ", FindDirtyHeaderID)

        Dim dt As Data.DataTable = dat.GetData(sql)

        If dt.Rows(0)("Active") Is DBNull.Value Then
            Me.fv.ChangeMode(FormViewMode.Edit)
        Else
            Me.fv.ChangeMode(FormViewMode.ReadOnly)
        End If

        Me.fv.DataSource = dt
        Me.fv.DataBind()

        Me.BindDirtyItemForEdit(FindDirtyHeaderID)
    End Sub

#End Region

#Region "繫結資料"
    Private Sub BindDirtyItemForInsert()
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        'sql.AppendFormat("select Value as DirtyItemID ")
        'sql.AppendFormat(", [Text] as DirtyItemName ")
        'sql.AppendFormat("from CodeMapping ")
        'sql.AppendFormat(" where TableName='FindDirtyItem' and ColumnName='DirtyItemName' ")

        sql.AppendFormat("select a.Value,a.Text, ")
        sql.AppendFormat("b.Value as SubValue,b.Text as SubText ")
        sql.AppendFormat("from CodeMapping a inner join CodeMapping b ")
        sql.AppendFormat("on a.TableName+'_'+a.ColumnName+'_'+a.Value = b.TableName ")
        sql.AppendFormat("where a.TableName = 'FindDirtyItem' and a.ColumnName='DirtyItemGroup' ")
        sql.AppendFormat("and a.Active = 'Y' And b.Active='Y' ")

        AddHandler TryCast(Me.fv.FindControl("gvDirtyItem"), GridView).RowDataBound, AddressOf Me.gv_RowDataBound
        TryCast(Me.fv.FindControl("gvDirtyItem"), GridView).DataSource = dat.GetData(sql)
        TryCast(Me.fv.FindControl("gvDirtyItem"), GridView).DataBind()

    End Sub

    Private Sub BindDirtyItemForEdit(ByVal FindDirtyHeaderID As String)
        Dim dat As New clsData
        Dim sql As New MyStringBuilder

        sql.AppendFormat("Select * from FindDirtyItem")
        sql.AppendFormat(" where FindDirtyHeaderID='{0}' ", FindDirtyHeaderID)

        Dim dt As Data.DataTable = dat.GetData(sql)

        Dim gvDirtyItem As GridView = TryCast(Me.fv.FindControl("gvDirtyItem"), GridView)


        Dim lblDirtyItemName As Label
        Dim cbList As CheckBoxList
        Dim lblValue As Label
        Dim lblSubValue As Label
        Dim txtNum As TextBox

        Dim ddlDirtyLevel As DropDownList

        For j As Integer = 0 To dt.Rows.Count - 1
            For i As Integer = 0 To gvDirtyItem.Rows.Count - 1
                lblDirtyItemName = TryCast(gvDirtyItem.Rows(i).FindControl("lblDirtyItemName"), Label)
                cbList = TryCast(gvDirtyItem.Rows(i).FindControl("cbList"), CheckBoxList)
                lblValue = TryCast(gvDirtyItem.Rows(i).FindControl("lblValue"), Label)
                lblSubValue = TryCast(gvDirtyItem.Rows(i).FindControl("lblSubValue"), Label)
                txtNum = TryCast(gvDirtyItem.Rows(i).FindControl("txtNum"), TextBox)

                ddlDirtyLevel = TryCast(gvDirtyItem.Rows(i).FindControl("ddlDirtyLevel"), DropDownList)


                If lblValue.Text = dt.Rows(j)("ItemGroupID").ToString() And lblSubValue.Text = dt.Rows(j)("ItemID").ToString() Then
                    cbList.Items(0).Selected = True
                    txtNum.Text = dt.Rows(j)("Amount").ToString()
                    ddlDirtyLevel.SelectedIndex = ddlDirtyLevel.Items.IndexOf(ddlDirtyLevel.Items.FindByValue(dt.Rows(j)("DirtyLevel").ToString()))
                    Exit For
                End If

            Next
        Next
    End Sub

    Private Sub BindTownID()
        Dim ddl As DropDownList = TryCast(fv.FindControl("ddlTownID"), DropDownList)

        ddl.DataTextField = "RegionName"
        ddl.DataValueField = "RegionID"
        ddl.DataSource = MyObj.GetTownData("TPC")
        ddl.DataBind()

        ddl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

    Private Sub BindVillageID()
        Dim ddl As DropDownList = TryCast(fv.FindControl("ddlVillageID"), DropDownList)

        ddl.DataTextField = "RegionName"
        ddl.DataValueField = "RegionID"
        ddl.DataSource = MyObj.GetVillageData(TryCast(fv.FindControl("ddlTownID"), DropDownList).SelectedValue)
        ddl.DataBind()
    End Sub

    Private Sub BindDirtyLevel(ByVal ddl As DropDownList)
        ddl.DataTextField = "Text"
        ddl.DataValueField = "Value"
        ddl.DataSource = MyObj.GetCodeMappingData("FindDirtyItem", "DirtyLevel")
        ddl.DataBind()

        ddl.Items.Insert(0, MyObj.GetDDLPleaseChooseItem())
    End Sub

#End Region

#Region "屬性"

    Public Property DataAccessMode() As My.DataAccessMode
        Get
            Return ViewState("DataAccessMode")
        End Get
        Set(ByVal value As My.DataAccessMode)
            ViewState("DataAccessMode") = value
        End Set
    End Property

    Public ReadOnly Property OtherDirtyItemName() As Boolean
        Get
            Dim gvDirtyItem As GridView = TryCast(Me.fv.FindControl("gvDirtyItem"), GridView)

            Dim ddlDirtyLevel As DropDownList

            For i As Integer = 0 To gvDirtyItem.Rows.Count - 1
                ddlDirtyLevel = TryCast(gvDirtyItem.Rows(i).FindControl("ddlDirtyLevel"), DropDownList)

                
            Next
            Return True
        End Get
    End Property

    '髒亂地區類別
    Public ReadOnly Property RegionType() As String
        Get
            Return TryCast(Me.fv.FindControl("SelfRegionType1"), WebForm_SelfManageWorkHeader_SelfRegionType).RegionText
        End Get
    End Property

    'VillageID
    Public ReadOnly Property VillageID() As String
        Get
            Return TryCast(Me.fv.FindControl("ddlVillageID"), DropDownList).SelectedValue
        End Get
    End Property

    'Location
    Public ReadOnly Property Location() As String
        Get
            Return TryCast(Me.fv.FindControl("txtLocation"), TextBox).Text.Trim()
        End Get
    End Property

    'Description
    Public ReadOnly Property Description() As String
        Get
            Return TryCast(Me.fv.FindControl("txtDescription"), TextBox).Text.Trim()
        End Get
    End Property

    'ReportDate
    Public ReadOnly Property ReportDate() As DateTime
        Get
            Return DateTime.Parse(TryCast(Me.fv.FindControl("txtReportDate"), WebForm_CommonControl_CalendarTextBox).Text)
        End Get
    End Property

    Public HaveItem As Boolean
    Private dirtyItemField As String(,)

    '髒亂種類/程度(DirtyItemName、DirtyLevel)
    Public Property DirtyItem() As String(,)
        Get
            Return dirtyItemField
        End Get
        Set(ByVal value As String(,))
            dirtyItemField = value
        End Set
    End Property

    '取髒亂種類/程度(DirtyItemName、DirtyLevel)
    Public Function SetDirtyItem() As String(,)
        Me.HaveItem = False
        Dim gvDirtyItem As GridView = TryCast(Me.fv.FindControl("gvDirtyItem"), GridView)
        Dim GetItem(gvDirtyItem.Rows.Count - 1, 3) As String
        Dim lblDirtyItemName As Label
        Dim cbList As CheckBoxList
        Dim lblValue As Label
        Dim txtNum As TextBox

        Dim ddlDirtyLevel As DropDownList


        'For i As Integer = 0 To gvDirtyItem.Rows.Count - 1
        '    lblDirtyItemName = TryCast(gvDirtyItem.Rows(i).FindControl("lblDirtyItemName"), Label)
        '    ddlDirtyLevel = TryCast(gvDirtyItem.Rows(i).FindControl("ddlDirtyLevel"), DropDownList)
        '    muOtherDirtyItem = TryCast(gvDirtyItem.Rows(i).FindControl("muOtherDirtyItem"), MultiView)
        '    If ddlDirtyLevel.SelectedValue <> Me.GetLocalResourceObject("MustSelect") Then
        '        Me.HaveItem = True
        '        If muOtherDirtyItem.Visible Then
        '            txtDirtyItemName = TryCast(muOtherDirtyItem.Views(0).FindControl("txtDirtyItemName"), TextBox)
        '            If txtDirtyItemName.Text.Trim() <> "" Then
        '                GetItem(i, 0) = txtDirtyItemName.Text.Trim()
        '                GetItem(i, 1) = ddlDirtyLevel.SelectedValue
        '            End If
        '        Else
        '            GetItem(i, 0) = lblDirtyItemName.Text
        '            GetItem(i, 1) = ddlDirtyLevel.SelectedValue
        '        End If
        '    End If
        'Next

        For i As Integer = 0 To gvDirtyItem.Rows.Count - 1
            lblDirtyItemName = TryCast(gvDirtyItem.Rows(i).FindControl("lblDirtyItemName"), Label)
            cbList = TryCast(gvDirtyItem.Rows(i).FindControl("cbList"), CheckBoxList)
            lblValue = TryCast(gvDirtyItem.Rows(i).FindControl("lblValue"), Label)
            txtNum = TryCast(gvDirtyItem.Rows(i).FindControl("txtNum"), TextBox)
            ddlDirtyLevel = TryCast(gvDirtyItem.Rows(i).FindControl("ddlDirtyLevel"), DropDownList)


            If ddlDirtyLevel.SelectedValue <> Me.GetLocalResourceObject("MustSelect") Then
                Me.HaveItem = True
                'If cbList.Items(0).Selected Then

                GetItem(i, 0) = lblValue.Text.Trim()
                GetItem(i, 1) = cbList.Items(0).Value
                GetItem(i, 2) = txtNum.Text.Trim()
                GetItem(i, 3) = ddlDirtyLevel.SelectedValue

                'End If

            End If
        Next
        Return GetItem

    End Function

    'PhotoPathID
    Public ReadOnly Property PhotoPathID() As String
        Get
            Return TryCast(Me.fv.FindControl("UploadPicControl1"), WebForm_CommonControl_UploadPicControl).strGUID
        End Get
    End Property

#End Region

#Region "Raise事件"
    Public Event Saving(ByVal sender As Object, ByVal e As EventArgs)
#End Region
End Class